//
//  DXMLSkaterEventObject.m
//  DerbyStatter
//
//  Created by glenn andreas on 1/29/14.
//  Copyright (c) 2014 glenn andreas. All rights reserved.
//

#import "DXMLSkaterEventObject.h"
#import "DXMLJamObject.h"
#import "DXMLPersonObject.h"
#import "DXMLPeriodObject.h"
#import "DXMLTeamObject.h"
#import "DXMLBoutObject.h"

@implementation DXMLSkaterEventObject
- (DXMLJamObject *) jam
{
    return [self enclosingParentOfClass:[DXMLJamObject class]];
}
- (DXMLPeriodObject *) period
{
    return [self enclosingParentOfClass:[DXMLPeriodObject class]];
}
- (DXMLBoutObject *) bout
{
    return [self enclosingParentOfClass:[DXMLBoutObject class]];
}
- (NSString *) skaterTeam
{
    if (self.skater) {
        return [NSString stringWithFormat:@"%@ %@",self.team.name, self.skater.number];
    } else {
        return [NSString stringWithFormat:@"%@ ???",self.team.name];
    }
}
// Synthesize to/from XML, but keep a cached copy - this is kind of ugly
- (void) parseSkaterId
{
    NSString *prop = [self.attributes objectForKey:@"id"];
    NSString *teamName = nil;
    NSString *skaterNumber = nil;
    NSString *skaterUUID = nil;
    if (prop.length) {
        skaterNumber = [[prop componentsSeparatedByString:@"_"] lastObject];
        teamName = [prop substringToIndex:prop.length - skaterNumber.length - 1];
    } else {
        prop = [self.attributes objectForKey:@"skater"];
        if (prop.length) {
            skaterUUID = prop;
        }
        prop = [self.attributes objectForKey:@"team"];
        if (prop.length) {
            teamName = prop;
        }
    }
    if (teamName) {
        for (DXMLTeamObject *team in self.bout.teams) {
            if ([team.name isEqualToString:teamName]) {
                _team = team;
                break;
            }
        }
    }
    if (skaterUUID) {
        for (DXMLTeamObject *team in self.bout.teams) {
            for (DXMLPersonObject *skater in team.skatersInRosterOrder) {
                if ([skater.uuid isEqualToString:skaterUUID]) {
                    _skater = skater;
                    break;
                }
            }
            if (_skater) break;
        }
    } else if (skaterNumber && _team) {
        for (DXMLPersonObject *skater in _team.skatersInRosterOrder) {
            if ([skater.number isEqualToString:skaterNumber]) {
                _skater = skater;
                break;
            }
        }
    }
}
- (void) saveSkaterId
{
    if (_skater && !_team) {
        for (DXMLTeamObject *team in self.enclosingBout.teams) {
            // don't use skaterForNumber since this skater's number may be in
            // more than one team
            if ([[team children] containsObject:_skater]) {
                _team = team;
                break;
            }
        }
    }
    if (_skater && _team) {
        //            [gxstream.writer addAttribute:_team.name name:@"team"];
        //            [gxstream.writer addAttribute:_skater.number name:@"number"];
        [self setAttribute:[NSString stringWithFormat:@"%@_%@",_team.name,_skater.number] forKey:@"id"];
        [self setAttribute:nil forKey:@"skater"];
        [self setAttribute:nil forKey:@"team"];
    } else if (_skater) {
        [self setAttribute:nil forKey:@"id"];
        [self setAttribute:_skater.uuid forKey:@"skater"];
        [self setAttribute:nil forKey:@"team"];
    } else if (_team) {
        [self setAttribute:nil forKey:@"id"];
        [self setAttribute:nil forKey:@"skater"];
        [self setAttribute:_team.name forKey:@"team"];
    }
}
@synthesize team = _team;
- (void) setTeam:(DXMLTeamObject *)team
{
    _team = team;
    if (![[_team children]containsObject:_skater])
        _skater = nil; // reset skater, not in team
    [self saveSkaterId];
}
- (DXMLTeamObject *) team
{
    if (_team)
        return _team;
    [self parseSkaterId];
    return _team;
}
@synthesize skater = _skater;
- (void) setSkater:(DXMLPersonObject *)skater
{
    if (![[_team children]containsObject:skater])
        _team = nil; // reset team, the skater not here
    _skater = skater;
    [self saveSkaterId];
}
- (DXMLPersonObject *) skater
{
    if (_skater)
        return _skater;
    [self parseSkaterId];
    return _skater;
}
@end

@implementation DXMLCallObject

@end
@implementation DXMLInjuryObject

@end
@implementation DXMLLeadObject


@end
@implementation DXMLLostLeadObject
@end

@implementation DXMLPassObject
DXMLINTATTR(number, Number)
DXMLINTATTR(score, Score)
- (BOOL) completed
{
    if ([self.attributes objectForKey:@"completed"])
        return [[self.attributes objectForKey:@"completed"] boolValue];
    return YES; // by default, it is completed
}
- (void) setCompleted:(BOOL)completed
{
    [self setBoolAttribute: completed forKey: @"completed"];
}
@end
NSString *DXMLLineUpPositionJammer = @"jammer";
NSString *DXMLLineUpPositionPivot = @"pivot";
NSString *DXMLLineUpPositionBlocker = @"blocker";
@implementation DXMLLineUpObject
DXMLBOOLATTRKEY(startsInBox, StartsInBox,@"start-in-box")
DXMLSTRINGATTR(position, Position)
@end
NSString *DXMLPenaltySeverityNone = @"no"; // no pass/no penalty
NSString *DXMLPenaltySeverityMinor = @"minor"; // no pass/no penalty
NSString *DXMLPenaltySeverityMajor = @"major"; // no pass/no penalty
NSString *DXMLPenaltySeverityExplusion = @"explusion"; // no pass/no penalty
@implementation DXMLPenaltyObject

@end

@implementation DXMLStarPassObject


@end


// For dealing with entering/exiting box
@implementation DXMLEnterBoxObject
DXMLINTATTR(pass, Pass)
DXMLINTATTR(duration, Duration)
@end
@implementation DXMLSubstitutionObject
- (DXMLPersonObject *) originalSkater
{
    return [(DXMLEnterBoxObject *)[self enclosingParentOfClass:[DXMLEnterBoxObject class]] skater];
}
@end

// why was there a premature exit of the box
NSString *DXMLExitBoxPrematureOfficial = @"official";
NSString *DXMLExitBoxPrematureSkater = @"skater";
NSString *DXMLExitBoxPrematureRescinded = @"rescinded";
NSString *DXMLExitBoxPrematureMistake = @"mistake";

@implementation DXMLExitBoxObject
DXMLINTATTR(pass, Pass)
DXMLINTATTR(duration, Duration)
DXMLSTRINGATTR(premature, Premature)
DXMLBOOLATTRKEY(noSkater, NoSkater, @"no-skater")
@end